package com.eclink.iot.tcp.virtualdevice.listener;

import com.eclink.iot.tcp.virtualdevice.VirtualDevice;
import com.eclink.iot.tcp.virtualdevice.VirtualDeviceManager;
import com.eclink.iot.tcp.virtualdevice.entity.Device;
import com.eclink.iot.tcp.virtualdevice.service.DeviceService;
import jakarta.annotation.Resource;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ApplicationStartListener {

    @Resource
    private DeviceService deviceService;

    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady() {
        System.out.println("Spring Boot application started.");

        List<Device> deviceList = deviceService.listAll();
        for (Device device : deviceList) {
            VirtualDevice proxy = VirtualDeviceManager.publicDevice(device);
            proxy.connect();
        }

        // 执行需要的逻辑
    }

}
